package com.woniuxy.member.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.cloud.mall95.log.Log;
import com.woniuxy.member.app.apiImpl.util.RequestUtil;
import com.woniuxy.member.client.MemberQueryClient;
import com.woniuxy.member.client.param.ExchangeCouponSearchParam;
import com.woniuxy.member.client.param.ExchangeFoodSearchParam;
import com.woniuxy.member.client.result.ExchangeCouponClientDto;
import com.woniuxy.member.client.result.ExchangeFoodClientDto;
import com.woniuxy.member.client.result.MemberClientDto;
import com.woniuxy.member.client.result.MemberCouponClientDto;
import com.woniuxy.member.infra.mapper.ExchangeCouponMapper;
import com.woniuxy.member.infra.mapper.ExchangeFoodMapper;
import com.woniuxy.member.infra.mapper.MemberCouponMapper;
import com.woniuxy.member.infra.model.ExchangeCoupon;
import com.woniuxy.member.infra.model.ExchangeFood;
import com.woniuxy.member.infra.model.Member;
import com.woniuxy.member.infra.model.MemberCoupon;
import com.woniuxy.member.service.ExchangeFoodService;
import com.woniuxy.member.service.MemberCouponService;
import com.woniuxy.member.service.MemberService;
import com.woniuxy.member.service.dto.ExchangeCouponDto;
import com.woniuxy.member.service.dto.ExchangeFoodDto;
import com.woniuxy.member.service.dto.MemberCouponDto;
import com.woniuxy.member.service.param.ExchangeCouponSearchServiceParam;
import com.woniuxy.member.service.param.ExchangeFoodSearchServiceParam;
import common.WoniuPage;
import exception.MallException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import result.CommonResultCodeEnum;
import result.Result;

import javax.validation.Valid;
import java.util.List;
@RequestMapping("/member")
@RestController
public class MemberQueryApiImpl implements MemberQueryClient {
    @Autowired
    private ExchangeFoodService exchangeFoodService;
    @Autowired
    private MemberCouponService memberCouponService;
    @Autowired
    private MemberCouponMapper memberCouponMapper;
    @Autowired
    private MemberService memberService;
    @Autowired
    private ExchangeFoodMapper exchangeFoodMapper;
    @Autowired
    private ExchangeCouponMapper exchangeCouponMapper;
    @Log("查询全部积分兑换菜品")
    @Override
    public Result<WoniuPage<ExchangeFoodClientDto>> getScoreExchangeFood(@Valid ExchangeFoodSearchParam exchangeFoodSearchParam) {
        if (exchangeFoodSearchParam.getPageIndex()==null||exchangeFoodSearchParam.getPageIndex()==0){
            exchangeFoodSearchParam.setPageIndex(1);
        }
        if (exchangeFoodSearchParam.getPageSize()==null||exchangeFoodSearchParam.getPageSize()==0){
            exchangeFoodSearchParam.setPageSize(3);
        }
        ExchangeFoodSearchServiceParam exchangeFoodSearchServiceParam = BeanUtil.toBean(exchangeFoodSearchParam, ExchangeFoodSearchServiceParam.class);
        exchangeFoodSearchServiceParam.setStoreId(RequestUtil.getStoreId());
        WoniuPage<ExchangeFoodDto> scoreExchangeFood = exchangeFoodService.getScoreExchangeFood(exchangeFoodSearchServiceParam);
        WoniuPage<ExchangeFoodClientDto> woniuPage = BeanUtil.toBean(scoreExchangeFood,WoniuPage.class);
        return Result.success(woniuPage);
    }

    @Log("查询全部积分兑换优惠卷")
    @Override
    public Result<List<ExchangeCouponClientDto>> getScoreExchangeCoupon(@Valid ExchangeCouponSearchParam exchangeCouponSearchParam) {
        ExchangeCouponSearchServiceParam exchangeCouponSearchServiceParam = BeanUtil.toBean(exchangeCouponSearchParam, ExchangeCouponSearchServiceParam.class);
        List<ExchangeCouponDto> list =exchangeFoodService.getScoreExchangeCoupon(exchangeCouponSearchServiceParam);
        List<ExchangeCouponClientDto> exchangeCouponClientDtos = BeanUtil.copyToList(list,ExchangeCouponClientDto.class);
        return Result.success(exchangeCouponClientDtos);
    }
    @Log("查询用户优惠卷")
    @Override
    public Result<List<MemberCouponClientDto>> getMemberCoupon() {
        List<MemberCouponDto> list=memberCouponService.getMemberCoupon(RequestUtil.getMemberId(),RequestUtil.getStoreId());
        List<MemberCouponClientDto> memberCouponClientDtos = BeanUtil.copyToList(list,MemberCouponClientDto.class);
        return Result.success(memberCouponClientDtos);
    }
    @Log("通过ID查询用户优惠卷")
    @Override
    public Result<List<MemberCouponClientDto>> getMemberCouponById(Integer couponId) {
        QueryWrapper<MemberCoupon> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("coupon_id",couponId).eq("use_state","UNUSE");
        List<MemberCoupon> memberCoupons = memberCouponMapper.selectList(queryWrapper);
        if (memberCoupons==null){
            throw new MallException("优惠卷不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        List<MemberCouponClientDto> memberCouponClientDtos = BeanUtil.copyToList(memberCoupons, MemberCouponClientDto.class);
        return Result.success(memberCouponClientDtos);
    }
    @Log("通过memberId查询Member")
    @Override
    public Result<MemberClientDto> getMemberByMemberId(Integer memberId) {
        Member member = memberService.getById(memberId);
        if (member==null){
            throw new MallException("用户不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        MemberClientDto memberClientDto = BeanUtil.toBean(member, MemberClientDto.class);
        return Result.success(memberClientDto);
    }
    @Log("通过skuId查询可兑换菜品")
    @Override
    public Result<ExchangeFoodClientDto> getMemberFoodBySkuId(Integer skuId) {
        QueryWrapper<ExchangeFood> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sku_id",skuId);
        ExchangeFood exchangeFood = exchangeFoodMapper.selectOne(queryWrapper);
        ExchangeFoodClientDto exchangeFoodClientDto = BeanUtil.toBean(exchangeFood, ExchangeFoodClientDto.class);
        return Result.success(exchangeFoodClientDto);
    }
    @Log("通过couponId查询可兑换优惠卷")
    @Override
    public Result<ExchangeCouponClientDto> getMemberCouponByCouponId(Integer couponId) {
        QueryWrapper<ExchangeCoupon> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("coupon_id",couponId);
        ExchangeCoupon exchangeCoupon = exchangeCouponMapper.selectOne(queryWrapper);
        ExchangeCouponClientDto exchangeCouponClientDto = BeanUtil.toBean(exchangeCoupon, ExchangeCouponClientDto.class);
        return Result.success(exchangeCouponClientDto);
    }


}
